<?php
// +----------------------------------------------------------------------
// | TwoThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.twothink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: YagamiZN 
// +----------------------------------------------------------------------

namespace app\admin\controller;
use alipay\alipaysdkPHP20171201160100\aop\AopClient;
use think\Log;

/**
 * 三方类库测试控制器
 */

class Plug extends Base {
    /**
     * Excel导出
     */
    public function ExcalExport(){
        $arr=db('car')->select();
        $title=['车名','新车','分期','发动机'];
        foreach ($arr as $v){
            $cdata[]=[$v['name'],$v['new_price'],$v['year_service'],$v['car_fdj']];
        }
        exportExcel($title, $cdata, '车档案', './',true);
    }
    /**
     * 数据导出
     * @param array $title   标题行名称
     * @param array $data   导出数据
     * @param string $fileName 文件名
     * @param string $savePath 保存路径
     * @param bool $isDown   是否下载  false--保存   true--下载
     * @return string   返回文件全路径
     * @throws PHPExcel_Exception
     * @throws PHPExcel_Reader_Exception
     * exportExcel(array('姓名','年龄'), array(array('a',21),array('b',23)), '档案', './', true)
     */
    function exportExcel($title=array(), $data=array(), $fileName='', $savePath='./', $isDown=false){
        Vendor('PHPExcel.Classes.PHPExcel');
        Vendor('PHPExcel.Classes.PHPExcel.IOFactory');
        $obj=new \PHPExcel();
        //横向单元格标识
        $cellName = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ');
    
        $obj->getActiveSheet(0)->setTitle('sheet1');   //设置sheet名称
        $_row = 1;   //设置纵向单元格标识
        if($title){
            $_cnt = count($title);
            $obj->getActiveSheet(0)->mergeCells('A'.$_row.':'.$cellName[$_cnt-1].$_row);   //合并单元格
            $obj->setActiveSheetIndex(0)->setCellValue('A'.$_row, '数据导出：'.date('Y-m-d H:i:s'));  //设置合并后的单元格内容
            $_row++;
            $i = 0;
            foreach($title AS $v){   //设置列标题
                foreach ($cellName as $vv){
                    $obj->setActiveSheetIndex(0)->getStyle($vv.'2')//设置字体
                    ->getFont()->setName('宋体') //字体
                    //->setSize(12) //字体大小
                    ->setBold(true); //字体加粗 
                    $obj->setActiveSheetIndex(0)->getColumnDimension($vv)//设置单元格宽
                    ->setAutoSize(true);
                    $obj->setActiveSheetIndex(0)
                    ->getStyle($vv.$_row)->getAlignment()//对齐方式
                    ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);//水平居中
                }
                $obj->setActiveSheetIndex(0)->setCellValue($cellName[$i].$_row, $v);
                $i++;
            }
            $_row++;
        }
        //填写数据
        if($data){
            $i = 0;
            foreach($data AS $_v){
                $j = 0;
                foreach($_v AS $_cell){
                    //填写数据之前可单独设置格式
                    $obj->getActiveSheet(0)->setCellValue($cellName[$j] . ($i+$_row), $_cell);
                    $j++;
                }
                $i++;
            }
        }
    
        //文件名处理
        if(!$fileName){
            $fileName = uniqid(time(),true);
        }
    
        $objWrite = \PHPExcel_IOFactory::createWriter($obj, 'Excel5');
    
        if($isDown){   //网页下载
            ob_end_clean();
            header('pragma:public');
            header("Content-Disposition:attachment;filename=$fileName.xls");
            header('Cache-Control: max-age=0');
            $objWrite->save('php://output');exit;
        }
    
        $_fileName = iconv("utf-8", "gb2312", $fileName);   //转码
        $_savePath = $savePath.$_fileName.'.xls';
        $objWrite->save($_savePath);
    
        return $savePath.$fileName.'.xls';
        //exportExcel(array('姓名','年龄'), array(array('a',21),array('b',23)), '档案', './', true);
    }
    /**
     * 微信支付
     */
    function wxpay(){
        $pay = & load_wechat('Pay');
        /**
         * 获取预支付ID
         * @param string $openid 用户openid，JSAPI必填
         * @param string $body 商品标题
         * @param string $out_trade_no 第三方订单号
         * @param int $total_fee 订单总价
         * @param string $notify_url 支付成功回调地址
         * @param string $trade_type 支付类型JSAPI|NATIVE|APP
         * @param string $goods_tag 商品标记，代金券或立减优惠功能的参数
         * @param string $fee_type 交易币种
         * @return bool|string
         */
        $prepay_id = $pay->getPrepayId(null, '扫码测试', time().mt_rand(1,999999), 5 * 100, url('index/index/notify', null, null, TRUE), 'NATIVE');
        dump($prepay_id);
        exit();
        if ($prepay_id === FALSE) {
            return ['code' => 'ERROR', 'info' => '创建预支付码失败，' . $pay->errMsg];
        }
        return ['code' => 'SUCCESS', 'info' => '创建预支付码成功！', 'data' => $prepay_id];
    }
    /**
     * 支付宝支付
     */
    function alipay(){
        $config=config('alipay');
        $config['return_url']='http://www.baidu.com';
        //var_dump($config);
        $params=[
            'subject'=>'test的 一幅幅三分得手',
            'out_trade_no'=>time().mt_rand(100000,999999),
            'total_amount' =>50,
        ];
        $aa=  \alipay\Pagepay::pay($params,$config) ;
        //$aa=  \alipay\Wappay::pay($params,$config) ;
    }
    
public function ali_notify()
    { // 异步订单结果通知
        $alipayNotify = new AopClient();
        $alipayNotify->alipayrsaPublicKey=config('alipay.alipay_public_key');
        $ppp['sign']='gfdgfdgfdgfdgdf';
        Log::init([
            'type' => 'File',
            'path' => LOG_PATH . '../paylog/'
        ]);
        Log::write('213gfdgfdgfdgfd嗷嗷嗷', 'log');
        $isOK=$alipayNotify->rsaCheckV1($ppp, NULL, "RSA2");
        var_dump($isOK);
        if ($isOK) {
            if (input('trade_status') == 'TRADE_FINISHED' || input('trade_status') == 'TRADE_SUCCESS') {
                // 处理支付成功后的逻辑业务
                Log::init([
                    'type' => 'File',
                    'path' => LOG_PATH . '../paylog/'
                ]);
                Log::write($isOK, 'log');
                return 'success';
            }
            return 'fail';
        }
        return 'fail';
    }
    public function chengchu(){
        echo 12000*5/6*0.6;
    }
    
}
